from flask import Blueprint
from .models import User
from ibm import db, render_template, redirect, request, url_for, \
    session,flash
from sqlalchemy import and_
login = Blueprint('login', __name__)


@login.route('/', methods=['GET'])
def login_get():
    return render_template('login.html')


@login.route('/', methods=['POST'])
def login_post():
    user_id  = request.form.get('user_id')
    password = request.form.get('user_passwd')
    user = User.query.filter(and_(User.user_id == user_id, User.password == password)).first()
    if user is not None:
        session['user_id'] = user.user_id
        return redirect(url_for('index.index_'))
    else:
        error = '用户名或密码错误，请重试'
        flash(error)
    return redirect(url_for('login.login_get'))